//
//  KKAlert.m
//  PhnixHome
//
//  Created by Levy Xu on 2018/1/3.
//  Copyright © 2018年 Levy Xu. All rights reserved.
//

#import "KKAlert.h"

@implementation KKAlert

+(QMUIAlertController *)warningWithText:(NSString *)text {
    return [KKAlert warningWithText:text ok:nil];
}

+(QMUIAlertController *)warningWithText:(NSString *)text ok:(void(^)(void))ok {
    return [KKAlert alertWithTitle:NSLocalizedString(@"tips_warning", nil) message:text cancel:nil ok:ok];
}

+(QMUIAlertController *)warningWithText:(NSString *)text cancel:(void(^)(void))cancel ok:(void(^)(void))ok {
    return [KKAlert alertWithTitle:NSLocalizedString(@"tips_warning", nil) message:text cancel:cancel ok:ok];
}


+(QMUIAlertController *)alertWithTitle:(NSString *)title message:(NSString *)message ok:(void(^)(void))ok {
    return [KKAlert alertWithTitle:title message:message cancel:nil ok:ok];
}

+(QMUIAlertController *)alertWithTitle:(NSString *)title message:(NSString *)message cancel:(void(^)(void))cancel ok:(void(^)(void))ok {
    QMUIAlertController *alert = [QMUIAlertController alertControllerWithTitle:title message:message preferredStyle:QMUIAlertControllerStyleAlert];
    QMUIAlertAction *okAction = [QMUIAlertAction actionWithTitle:NSLocalizedString(@"btn_ok", nil) style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
        !ok ?: ok();
    }];
    [alert addAction:okAction];
    
    if (cancel) {
        QMUIAlertAction *cancelAction = [QMUIAlertAction actionWithTitle:NSLocalizedString(@"btn_cancel", nil) style:QMUIAlertActionStyleCancel handler:nil];
        [alert addAction:cancelAction];
    }
    
    [alert showWithAnimated:YES];
    return alert;
}


+(QMUIDialogTextFieldViewController *)inputWithTitle:(NSString *)title text:(NSString *)text placeholder:(NSString *)placeholder ok:(void(^)(NSString *string))ok {
    QMUIDialogTextFieldViewController *dialog = [[QMUIDialogTextFieldViewController alloc] init];
    dialog.title = title;
    dialog.textField.text = text;
    dialog.textField.placeholder = placeholder;
    dialog.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [dialog addCancelButtonWithText:NSLocalizedString(@"btn_cancel", nil) block:nil];
    [dialog addSubmitButtonWithText:NSLocalizedString(@"btn_ok", nil) block:^(QMUIDialogViewController *dialogViewController) {
        QMUIDialogTextFieldViewController *alert = (QMUIDialogTextFieldViewController *)dialogViewController;
        
        !ok ?: ok(alert.textField.text);
        
        [alert hide];
    }];
    [dialog show];
    return dialog;
}

@end
